package com.jlcindia.hibernate;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.type.IntegerType;
import org.hibernate.type.LongType;
import org.hibernate.type.StringType;

import com.jlcindia.util.HibernateUtil;

public class Client2 {
	public static void main(String args[]) {
		Transaction tx = null;
		try {
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();

			System.out.println("1. All the Contacts");
			String sql1 = "select * from contacts cts";
			SQLQuery sq = session.createSQLQuery(sql1);
			sq = sq.addScalar("contactId", IntegerType.INSTANCE);
			sq = sq.addScalar("firstName", StringType.INSTANCE);
			sq = sq.addScalar("lastName", StringType.INSTANCE);
			sq = sq.addScalar("email", StringType.INSTANCE);
			sq = sq.addScalar("phone", LongType.INSTANCE);
			sq = sq.addScalar("dob", StringType.INSTANCE);
			sq = sq.addScalar("status", StringType.INSTANCE);
			List<Object[]> clist = sq.list();
			for (Object[] obj : clist) {
				for (Object o : obj) {
					System.out.println(o + "\t");
				}
				System.out.println(" ");
			}

			System.out.println("2. Email of All the Contacts");
			String sql4 = "select cts.email from contacts cts";
			SQLQuery sq1 = session.createSQLQuery(sql4);
			sq1 = sq1.addScalar("email", StringType.INSTANCE);
			List<String> ems = sq1.list();
			for (String str : ems) {
				System.out.println(str);
			}

			tx.commit();
			session.close();

		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		}
	}
}
